package com.neuedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.entity.Register;
import com.neuedu.mapper.RegisterMapper;
import com.neuedu.service.RegisterService;
import com.neuedu.vo.RegisterCondition;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class RegisterServiceImpl extends ServiceImpl<RegisterMapper, Register> implements RegisterService {

    @Resource
    private RegisterMapper registerMapper;

    @Override
    public boolean add(Register register) {

        //格式：yyyyMMddNXX = 看诊日期yyyyMMdd + 午别N(0 : 上午  1 ：下午)  + 两位的顺序号XX(01-99)
        String registerId = null;  //202310121  01 20231012107

        //构造查询条件的值
        String regDateAndNoon = new SimpleDateFormat("yyyyMMdd").format(register.getRegisterDate()) + register.getRegisterNoon();

        //方式1---倒序排序 + limit 1
        QueryWrapper<Register> wrapper = new QueryWrapper<>();

        wrapper.likeRight("register_id", regDateAndNoon);
        wrapper.orderByDesc("register_id");
        wrapper.last("limit 1");

        Register maxRegister = registerMapper.selectOne(wrapper);
        if(maxRegister==null){   //不存在最大的病历号
            System.out.println("maxRegisterId还不存在");
            registerId = regDateAndNoon + "01";   //从01开始计数
        }else{
            String maxRegisterId = maxRegister.getRegisterId(); //202310121  07  --> 202310121   08
            System.out.println("maxRegisterId = " + maxRegisterId);

            int newId = Integer.parseInt(maxRegisterId.substring(9)) + 1;
            if(newId < 10){
                registerId = regDateAndNoon + "0" + newId;
            }else{
                registerId = regDateAndNoon + newId;
            }
        }

/*
        //方式2---max() + map
        QueryWrapper<Register> wrapper = new QueryWrapper<>();

        wrapper.likeRight("register_id", regDateAndNoon);
        wrapper.select("max(register_id) as max_register_id");
        //wrapper.select("register_id, patient_name, register_amount");
        //wrapper.select("max(register_id) as max_register_id, min(register_id) as min_register_id, count(*) as register_count");
        List<Map<String, Object>> maps = registerMapper.selectMaps(wrapper);

        System.out.println("maps=" + maps);

        if (maps.get(0) == null) {   //不存在最大的病历号
            System.out.println("maxRegisterId还不存在");
            registerId = regDateAndNoon + "01";   //从01开始计数
        } else {
            String maxRegisterId = (String) maps.get(0).get("max_register_id"); //202310121  07  --> 20231012108
            System.out.println("maxRegisterId = " + maxRegisterId);

            int newId = Integer.parseInt(maxRegisterId.substring(9)) + 1;
            if (newId < 10) {
                registerId = regDateAndNoon + "0" + newId;
            } else {
                registerId = regDateAndNoon + newId;
            }
        }
*/
        System.out.println("新生成的主键=" + registerId);

        //填充病历号主键
        register.setRegisterId(registerId);

        //填充挂号时间
        register.setRegisterTime(new Date());

        //填充挂号状态
        register.setStatus(1);

        System.out.println("填充数据后=" + register);

        //添加到数据库中
        return this.save(register);

    }

    @Override
    public List<Register> list(RegisterCondition registerCondition) {

        //获取当前时间，截取日期部分
        String registerDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());

        //确定当前时间的午别
        Calendar calendar = Calendar.getInstance();
        int registerNoon = calendar.get(Calendar.AM_PM); //上午返回0 下午返回1

        /*List<Register> registerList = null;

        if(registerCondition.getStatus()==null) {
            registerList = this.query()
                    .like("register_id", registerCondition.getRegisterId())
                    .like("patient_name", registerCondition.getPatientName())
                    .eq("doctor_id", registerCondition.getDoctorId())
                    .eq("register_date", registerDate)
                    .eq("register_noon", registerNoon)
                    .list();
        }else{
            registerList = this.query()
                    .like("register_id", registerCondition.getRegisterId())
                    .like("patient_name", registerCondition.getPatientName())
                    .eq("doctor_id", registerCondition.getDoctorId())
                    .eq("register_date", registerDate)
                    .eq("register_noon", registerNoon)
                    .eq("status", registerCondition.getStatus())
                    .list();
        }
*/

        //构造查询条件包裹器
        QueryWrapper<Register> wrapper = new QueryWrapper<>();

        wrapper.like("register_id", registerCondition.getRegisterId())
                .like( "patient_name", registerCondition.getPatientName())
                .eq("doctor_id", registerCondition.getDoctorId())
                .eq("register_date", registerDate)
                .eq("register_noon", registerNoon);

        if(registerCondition.getStatus()!=null) {
            wrapper.eq("status", registerCondition.getStatus());
        }

        List<Register> registerList = registerMapper.selectList(wrapper);

        return registerList;
    }

    @Override
    public Map<String, Integer> getRegisterCount(int doctorId) {

        //获取当前时间，截取日期部分
        String registerDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());

        //确定当前时间的午别
        Calendar calendar = Calendar.getInstance();
        int registerNoon = calendar.get(Calendar.AM_PM); //上午返回0 下午返回1

        //查询新挂号的人数
        Integer newCount = this.query()
                .eq("doctor_id", doctorId)
                .eq("register_date", registerDate)
                .eq("register_noon", registerNoon)
                .eq("status", 1)
                .count();

        //查询已接诊的人数
        Integer receiveCount = this.query()
                .eq("doctor_id", doctorId)
                .eq("register_date", registerDate)
                .eq("register_noon", registerNoon)
                .eq("status", 2)
                .count();

        //查询已结束的人数
        Integer finishCount = this.query()
                .eq("doctor_id", doctorId)
                .eq("register_date", registerDate)
                .eq("register_noon", registerNoon)
                .eq("status", 3)
                .count();

        //累加总人数
        int totalCount = newCount + receiveCount + finishCount;

        //将查询结果封装到Map中
        Map<String, Integer> map = new HashMap<>();

        map.put("newCount", newCount);
        map.put("receiveCount", receiveCount);
        map.put("finishCount", finishCount);
        map.put("totalCount", totalCount);

        return map;
    }

    @Override
    public boolean modifyRegister(String registerId, String speak) {

        //方式1---先根据主键查询，再进行全字段更新
        /*Register register = registerService.getById(registerId);

        //更新主诉和看诊状态
        register.setSpeak(speak);
        register.setStatus(2);

        return this.updateById(register);
        */

        //方式2---根据主键，按需字段更新
        UpdateWrapper<Register> wrapper = new UpdateWrapper<>();

        wrapper.eq("register_id", registerId);
        wrapper.set("speak", speak);
        wrapper.set("status", 2);

        return this.update(wrapper);
    }

    @Override
    public boolean insureRegister(String registerId, String result, int diseaseId) {

        UpdateWrapper<Register> wrapper = new UpdateWrapper<>();

        wrapper.eq("register_id", registerId);

        wrapper.set("result", result);
        wrapper.set("disease_id", diseaseId);
        wrapper.set("status", 3);

        return this.update(wrapper);
    }
}

